package com.key.puzzlemodel.view

import android.content.Context
import android.graphics.Canvas
import android.graphics.Outline
import android.graphics.Path
import android.graphics.RectF
import android.os.Build
import android.util.AttributeSet
import android.view.View
import android.view.ViewOutlineProvider
import androidx.appcompat.widget.AppCompatImageView
import com.key.puzzlemodel.R

/**
 *
 * @功能描述:     圆角的imageview
 * @创建时间:     2022/1/10
 * @创建者:       阳
 * @修改时间:     2022/1/10
 * @修改者:       阳
 */
class CircleImageView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) :
    AppCompatImageView(context, attrs) {
    private var mRadius: Int = 0
    private var viewOutlineProvider: ViewOutlineProvider? = null
    private var path: Path? = null
    private var rect: RectF? = null

    init {
        val obtainStyledAttributes =
            context.obtainStyledAttributes(attrs, R.styleable.CircleImageView)
//        mRadius =
//            obtainStyledAttributes.getDimension(R.styleable.CircleImageView_radius, 0.01f)
//                .toInt()
        obtainStyledAttributes.recycle()
//        setRound(mRadius)

    }

    //设置圆角图片
    fun setRound(radius: Int) {
        val isChange = radius != mRadius
        mRadius = radius
        if (path == null) {
            path = Path()
        }
        if (rect == null) {
            rect = RectF()
        }
        if (mRadius != 0) {
            if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
                if (viewOutlineProvider == null) {
                    viewOutlineProvider = object : ViewOutlineProvider() {
                        override fun getOutline(view: View?, outline: Outline?) {
                            val width = width
                            val height = height
                            outline?.setRoundRect(0, 0, width, height, radius.toFloat())
                        }
                    }
                }
                outlineProvider = viewOutlineProvider
                clipToOutline = true
            }
            val width = width.toFloat()
            val height = height.toFloat()
            rect?.set(0f, 0f, width, height)
            path?.reset()
            rect?.let {
                path?.addRoundRect(it, mRadius.toFloat(), mRadius.toFloat(), Path.Direction.CW)
            }
        } else {
            if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
                clipToOutline = false
            }
        }

        if (isChange) {
            if (Build.VERSION_CODES.LOLLIPOP <= Build.VERSION.SDK_INT) {
                invalidateOutline()
            }
        }

    }

    override fun draw(canvas: Canvas?) {
        var clip = false
        if (Build.VERSION_CODES.LOLLIPOP > Build.VERSION.SDK_INT && mRadius > 0) {
            clip = true
            canvas?.save()
            path?.let { canvas?.clipPath(it) }
        }
        super.draw(canvas)
        if (clip) {
            canvas?.restore()
        }
    }


}